//
//  moviecollectionViewController.m
//  ui豆瓣
//
//  Created by lirui on 15/12/26.
//  Copyright © 2015年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "moviecollectionViewController.h"
#import "movieView.h"
#import "dengluViewController.h"
#import "UIImageView+WebCache.h"
#import "DataBaseManger.h"
@interface moviecollectionViewController ()
@property(nonatomic, retain)movieView *user;
@property(nonatomic, retain)NSMutableDictionary *dic;

@end

@implementation moviecollectionViewController
-(void)viewWillAppear:(BOOL)animated
{
    
    NSString *states = [[NSUserDefaults standardUserDefaults] objectForKey:@"states"];
    if ([states isEqualToString:@"yes"]) {
        self.navigationItem.rightBarButtonItem =[[ UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(right:)];
    }else
    {
        self.navigationItem.rightBarButtonItem =[[ UIBarButtonItem alloc] initWithTitle:@"登录" style:UIBarButtonItemStylePlain target:self action:@selector(denglu:)];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [ UIColor whiteColor];
    //xib
    self.user = [[[ NSBundle mainBundle]  loadNibNamed:@"moviewView" owner:self options:nil] lastObject];
    self.user.center = CGPointMake(self.view.center.x, self.view.center.y);
    [self.view addSubview:self.user];
    
    [self datahandler];
    
  }
-(void)datahandler
{
    
    
    NSString *str = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId=%@",self.Mov.movieId];
    NSLog(@"%@, %@", self.Mov, self.Mov.movieId);
    NSString *urlstr =[ str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:str]];
    NSURL *url =[ NSURL URLWithString:urlstr];
    NSURLRequest *request =[ NSURLRequest requestWithURL:url];
    NSURLSession *session = [ NSURLSession sharedSession];
    NSURLSessionDataTask *task =[ session  dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            id result = [ NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            self.dic =[ NSMutableDictionary dictionary];
                  self.dic = result[@"result"];
            NSLog(@"dic %@", self.dic);
                      self.user.rating.text = self.dic[ @"rating"];
                        self.user.time.text = self.dic[ @"year"];
                        self.user.runtime.text = self.dic[ @"runtime"];
                        self.user.kind.text = self.dic[@"genres"];
                        self.user.area.text = self.dic[@"country"];
                        self.user.movieqingjie.text = self.dic[ @"plot_simple"];
                        //self.actor.text = @"制作人";
                        self.user.actor .text=self.dic[@"actors"];
                        //self.movieqingjie.text = @"电影情节";
                    [self.user.imagephoto sd_setImageWithURL:[ NSURL URLWithString:self.dic[@"poster"]]];
                        self.navigationItem.title = self.dic[@"title"];
        });
    }];
    
    [task resume];
    

    
    
}
//左按钮方法
-(void)left:(UINavigationItem *)left
{
    [self.navigationController popViewControllerAnimated:YES];
}
//右按钮方法
-(void)denglu:(UINavigationItem *)denglu
{ 
    
    
    
    dengluViewController *denglu1 = [[ dengluViewController alloc] init];
    UINavigationController *navi = [[ UINavigationController alloc] initWithRootViewController:denglu1];
    [self presentViewController:navi animated:YES completion:^{
        NSLog(@"进入登录界面");
    }];
    
}
-(void)right:(UINavigationItem *)right
    {
        DataBaseManger *dbmanger =[ DataBaseManger shareInstance ];
        [dbmanger openDB];
        //[[DataBaseManger shareInstance]dropTable];
        [[DataBaseManger shareInstance] createTable];
        NSMutableArray *arr =[[ DataBaseManger shareInstance] select];
        for (NSDictionary *dic in arr) {
            if ([dic[@"title" ] isEqualToString:_dic[@"title"]]) {
                NSLog(@"已经收藏了");
                UIAlertController *a = [UIAlertController alertControllerWithTitle:@"提示" message:@"已经收藏过了" preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *b = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                    NSLog(@"取消");
                    
                }];
                UIAlertAction *c = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                    NSLog(@"确定");
                }];
                
                [a addAction:b];
                [a addAction:c];
                [self presentViewController:a animated:YES completion:^{
                    
                }];

                return;
            }
        }
        [[DataBaseManger shareInstance] insertMovie:self.dic];
       
     
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
